package com.cy.milktea.controller;


import com.cy.milktea.pojo.QueuingOrder;
import com.cy.milktea.pojo.System.JsonResult;
import com.cy.milktea.pojo.Order;
import com.cy.milktea.service.OrderService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;


@RestController
public class OrderController implements InitializingBean {

    private final Object o = new Object();  //----->对象锁
    private final Timer timer = new Timer();  //--->计时器:用于定期向浏览器刷新订单数据
    private boolean isQuery = true;  //------------>查询状态
    private List<QueuingOrder> currentOrderQueue;
    @Autowired
    private OrderService orderService;

    @Override
    public void afterPropertiesSet() {
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                synchronized (o) {
                    isQuery = true;  //设置可查询
                    o.notifyAll();  //唤醒其他线程
                }
            }
        }, 1000, 1000);  //每1秒刷新一次
    }

    /* web第一次查询 */
    @GetMapping("/order/queue/first")
    public JsonResult firstQueryQueue() {
        return JsonResult.ok("初始查询成功!", orderService.queryCurrentQueue());
    }

    /* 订单队列查询 */
    @GetMapping("/order/queue")
    public JsonResult queryQueue() throws InterruptedException {
        synchronized (o) {
            o.wait(); //等待唤醒
            if (isQuery) {  // 防止多次查询,减少开销
                isQuery = false;
                currentOrderQueue = orderService.queryCurrentQueue();  // 查询订单队列
            }
        }
        return JsonResult.ok("订单队列查询成功!", currentOrderQueue);
    }

    /* 顾客直接下订单,不涉及支付 */
    @PostMapping("/consumer/order/toOrder")
    public JsonResult toOrder(@RequestBody QueuingOrder order) {
        order = orderService.createOnQueue(order);  // 生成订单信息
        synchronized (o) {
            isQuery = true;
            o.notifyAll();  //唤醒阻塞中的线程
        }
        return JsonResult.ok("下单成功,请等待饮品完成!", order);
    }

    /* 服务员完成订单 */
    @PutMapping("/order")
    public JsonResult finish(@RequestBody QueuingOrder order) {
        orderService.finish(order);
        return JsonResult.ok("完成订单!");
    }


    /* 查询历史订单 */
    @GetMapping("/order/{orderID}/{dateStr}/{sort}")
    public JsonResult query(@PathVariable String orderID,
                            @PathVariable String dateStr,
                            @PathVariable String sort) {
        if (orderID != null) orderID = "".equals(orderID.trim()) ? null : orderID.trim();
        Date minDate = null;
        Date maxDate = null;
        if (StringUtils.hasLength(dateStr)) {
            try {
                minDate = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
                maxDate = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss").parse(dateStr + "-23:59:59");

            } catch (ParseException e) {
            }
        }
        sort = "1".equals(sort) ? "DESC" : "ASC";
        return JsonResult.ok(orderService.queryHistory(orderID, minDate, maxDate, sort));
    }
}

























